<?php
/**
 * 导航内容Service
 * Date: 2025/09/10
 */

namespace App\Services\Pc;

use App\Models\CategoryModel;
use App\Models\ContentModel;
use App\Models\ViewModel;
use App\Services\BaseService;
use Illuminate\Support\Facades\DB;

class ContentService extends BaseService
{
    /**
     * 内容列表
     * @date 2025/11/05
     * @return \Illuminate\Http\JsonResponse
     */
    public function list()
    {
        // 内容列表查询
        $contentList = ContentModel::query()
            ->where('status', ContentModel::STATUS_OPEN)
            ->orderBy('content.sort', 'asc')
            ->orderBy('content.id', 'desc')
            ->get(['id', 'category_id', 'logo', 'title', 'description'])
            ->toArray();
        $contentGroupList = [];
        foreach ($contentList as $contentItem) {
            if (!isset($contentGroupList[$contentItem['category_id']])) {
                $contentGroupList[$contentItem['category_id']] = [];
            }
            $contentGroupList[$contentItem['category_id']][] = $contentItem;
        }

        // 分类列表查询
        $categoryList = CategoryModel::query()
            ->where('status', CategoryModel::STATUS_OPEN)
            ->orderBy('sort', 'asc')
            ->orderBy('id', 'asc')
            ->get(['id', 'pid', 'icon', 'name'])
            ->toArray();
        foreach ($categoryList as &$categoryItem) {
            $categoryItem['content_list'] = $contentGroupList[$categoryItem['id']] ?? [];
        }
        return $this->success(buildTree($categoryList, 'id'));
    }

    /**
     * 内容详情
     * @date 2025/11/05
     * @param int $id 内容ID
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail($id)
    {
        // 内容详情
        $contentInfo = ContentModel::query()
            ->select(['id', 'category_id', 'logo', 'title', 'keywords', 'description', 'jump', 'jump_link'])
            ->where('status', ContentModel::STATUS_OPEN)
            ->where('content.id', $id)
            ->first();
        if (empty($contentInfo)) {
            return $this->failed('导航不存在');
        }

        // 查询分类信息
        $categoryPath = [];
        $category = CategoryModel::query()->where('id', $contentInfo->category_id)->first();
        if ($category) {
            if ($category->pid > 0) {
                $parent = CategoryModel::query()->where('id', $category->pid)->first();
                if ($parent) {
                    $categoryPath[] = $parent->name;
                }
            }
            $categoryPath[] = $category->name;
        }
        $contentInfo->category_path = $categoryPath;


        // 查询热门内容ID
        $hotContentIds = ViewModel::query()
            ->select('data_id', DB::raw('SUM(pv) as total_pv'))
            ->where('type', ViewModel::TYPE_CONTENT)
            ->groupBy('data_id')
            ->orderByDesc('total_pv')
            ->limit(15)
            ->pluck('data_id')
            ->toArray();

        // 查询热门内容列表
        $hotContentList = [];
        $contentList = ContentModel::query()
            ->where('status', ContentModel::STATUS_OPEN)
            ->whereIn('id', $hotContentIds)
            ->get(['id', 'logo', 'title', 'keywords', 'description'])
            ->toArray();
        $contentList = array_column($contentList, null, 'id');
        foreach ($hotContentIds as $hotContentId) {
            if (isset($contentList[$hotContentId])) {
                $hotContentList[] = $contentList[$hotContentId];
            }
        }
        $contentInfo->hot_content_list = $hotContentList;
        return $this->success($contentInfo);
    }
}
